//
//  PayTypeMenu.m
//  PigMarket
//
//  Created by mac on 2019/7/16.
//  Copyright © 2019 pigmarket.oc. All rights reserved.
//

#import "PayTypeMenu.h"
#import "PayTypeMenuCell.h"

@interface  PayTypeMenu ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
/**
 bgview
 */
@property (nonatomic , strong) UIView *bgView;
/**
 headerView
 */
@property (nonatomic , strong) UIView *headerView;
/**
 title
 */
@property (nonatomic , strong) UIView *title;
/**
 取消
 */
@property (nonatomic , strong) UIButton *cancelBtn;
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
@end
@implementation PayTypeMenu


#pragma mark - init method
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
        [self initLayout];
        [self initData];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.myTableView];
}

- (void)initLayout {
    // 布局子控件
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(ScaleHeight(162));
    }];
}
- (void)initData {
    NSArray *arr = @[@{@"icon":@"pay_weichat",@"title":@"微信支付",@"rightBtn":@"selectBtn",@"isSelect":@"NO"},
                     @{@"icon":@"pay_alipay",@"title":@"支付宝",@"rightBtn":@"selectBtn",@"isSelect":@"No"}];
    [self.dataArr addObjectsFromArray:arr];
}
#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PayTypeMenuCell *cell = [PayTypeMenuCell cellWithTableView:tableView];
    if (self.dataArr.count > indexPath.row) {
        cell.infoDic = self.dataArr[indexPath.row];
        return cell;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PayType paytype = PayTypeWechat;
    if (indexPath.row == 0) {
        paytype = PayTypeWechat;
    } else if (indexPath.row == 1){
        paytype = PayTypeAlipay;
    }
    
    if (self.selectPayTypeBlock) {
        self.selectPayTypeBlock(paytype);
    }
    [self hide];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.myTableView]) {
        return NO;
    }
    return YES;
}
#pragma mark - target action
- (void)cancelBtnClcik {
    [self removeFromSuperview];

}
#pragma mark - private method
- (void)hide {
    [self removeFromSuperview];
}
#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.rowHeight = ScaleHeight(54);
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.tableHeaderView = self.headerView;
        _myTableView.scrollEnabled = NO;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
        tap.delegate = self;
        [_bgView addGestureRecognizer:tap];
    }
    return _bgView;
}
- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 52)];
        _headerView.backgroundColor = [UIColor whiteColor];
        
        UILabel *title = [[UILabel alloc] init];
        title.text = @"选择充值方式";
        title.textColor = [UIColor colorWithHexString:@"#24253D"];
        title.font = [UIFont systemFontOfSize:16];
        [_headerView addSubview:title];
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.top.bottom.mas_equalTo(0);
        }];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor colorWithHexString:@"#24253D"] forState:UIControlStateNormal];
        cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [cancelBtn addTarget:self action:@selector(cancelBtnClcik) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:cancelBtn];
        [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-15);
            make.top.bottom.mas_equalTo(0);
        }];
    }
    return _headerView;
}
@end
